package sg.edu.nus.iss.cats.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;

import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.controller.UserSession;

public class LogoutAction extends Action {

	public ActionForward execute(ActionMapping mapping,
								 ActionForm form,
								 HttpServletRequest request,
								 HttpServletResponse response)
						 throws IOException, ServletException {

		// Extract attributes we will need
		HttpSession session = request.getSession();
		UserSession userSession =
			(UserSession) session.getAttribute("USERSESSION");

		User user = userSession.getUser();

		// Process this user logout
		if (user != null) {
			servlet.log(
				"LogoutAction: User '"
					+ user.getName()
					+ "' logged off in session "
					+ session.getId());
		} else {
			servlet.log(
				"LogoutActon: User logged off in session " + session.getId());
		}
		session.removeAttribute("USERSESSION");
		session.invalidate();

		// Forward control to the specified success URI
		return (mapping.findForward("success"));
	}
}